// pages/chartere_bus6/line_car6-12/line_car6-12.js
var httpUtil = require('../../utils/httpUtil.js');
var util = require('../../utils/util.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsList:{},
    specificationModel:[],
    date:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this; 

    var pages = getCurrentPages();

    var prevPage = pages[pages.length - 2];  //上一个页面
    this.setData({
      date: prevPage.data.date
    })
    that.getList();
    
  },  getList: function () {
    var that = this;
    httpUtil.getHttp("/goods_travel/list?type=6", {}, function (data) {
      var specificationModel=[];
      for (var i = 0; i < data.length;i++){
        var item = data[i];
        httpUtil.getHttp("/goods_travel/get/" + item.id + "/" + item.goodsType + "/specification", {}, function (res) {
 
          specificationModel.push(res);
           that.setData({
            specificationModel: specificationModel
          })   
      })
 
      }
      that.setData({
        goodsList: data
      })
    })
  } ,

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  addShopping: function (e) {
    var that = this;
    var shoppingList = [];
    var userInfo = util.getKey("userInfo");
    var id = e.target.dataset.id;
    var sId = e.target.dataset.sid;
    if (id == undefined || sId==undefined){
        return;
    }

    var shoppingList = [{ "num": 1, "specificationId": sId, "goodsId": id, "userId": userInfo.id, "del_status": 0, "date": that.data.date }]
    httpUtil.postHttp("/order/addShoppingList", { list: shoppingList }, function (res) {
      console.log(res);
      var ids = "";
      for (var i = 0; i < res.length; i++) {
        if (i != 0) {
          ids += ","
        }

        ids += res[i].id;
      }
      console.log(ids);
      wx.navigateTo({
        url: '../reserve1/reserve1?id=' + ids
      });

    });
    console.log(shoppingList);
  }
})